﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EChartTest.Models;
using EChartTest.Models.Enum;

namespace EChartTest.Controllers
{
    public class EchartController : Controller
    {

        
        // GET: Echart
        public ActionResult Index()
        {
            dynamic obj = new ExpandoObject();
            obj.Title = "测试统计";
            obj.ChartType = EChartType.Bar;//统计类型
            return View(obj);
        }
        public ActionResult GetEchartData()
        {
            var reslut = new List<dynamic>();
            //若是数据库数据,请转换成dynamic类型
            using (var datacontext = new Test())
            {
                //var date = Convert.ToDateTime("2016-01-31");//筛选条件
                //var date2 = Convert.ToDateTime("2017-03-31");//筛选条件
                //var list =
                // (from x in datacontext.EveryDayRead_Site_ArticleInfo select x)/*.Where(x => x.CreateTime <= date2 && x.CreateTime >= date)*/.GroupBy(x => x.CreateTime.Month);
                #region 柱状图,折线图
                //var data = new List<dynamic>();
                //foreach (var item in list)
                //{
                //    data.Add(new
                //    {
                //        Key = item.Key.ToString() + "月",
                //        Value = item.ToList().Count
                //    });
                //}
                //var obj = new
                //{
                //    Key = "文章信息统计",
                //    Value = 0,
                //    Data = data
                //};
                //reslut.Add(obj);
                #endregion
                #region 饼图
                //foreach (var item in list)
                //{
                //    reslut.Add(new
                //    {
                //        Key = item.Key.ToString() + "月",
                //        Value = item.ToList().Count
                //    });

                //}
                #endregion
            }
            #region 静态数据

            dynamic obj = new
            {
                Key = "分类数据1",
                Value = 124,
                Data = new List<dynamic>() { new { Key = "一月", Value = 120 }, new { Key = "二月", Value = 520 }, new { Key = "三月", Value = 110 } }//用于柱状图或者折线图
            };
            dynamic obj2 = new
            {
                Key = "分类数据2",
                Value = 55,
                Data = new List<dynamic>() { new { Key = "一月", Value = 330 }, new { Key = "二月", Value = 440 }, new { Key = "三月", Value = 550 } }//用于柱状图或者折线图
            };
            dynamic obj3 = new
            {
                Key = "分类数据3",
                Value = 33,
                Data = new List<dynamic>() { new { Key = "一月", Value = 335 }, new { Key = "二月", Value = 240 }, new { Key = "三月", Value = 150 } }//用于柱状图或者折线图
            };
            reslut.Add(obj);
            reslut.Add(obj2);
            reslut.Add(obj3);
            #endregion
            return Json(reslut, JsonRequestBehavior.AllowGet);
        }
    }
}